package com.wnxy.sddfp.search.service.impl;

import com.wnxy.sddfp.common.model.page.Pages;
import com.wnxy.sddfp.search.model.query.BorrowSearchQuery;
import com.wnxy.sddfp.search.model.vo.BorrowVO;
import com.wnxy.sddfp.search.service.BorrowService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BorrowServiceImpl implements BorrowService {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public Pages<BorrowVO> search(BorrowSearchQuery borrowSearchQuery) {
        // 条件构造器
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        if (StringUtils.hasText(borrowSearchQuery.getName())) {
            builder.withQuery(QueryBuilders.matchQuery("name", borrowSearchQuery.getName()));
        }
        if (StringUtils.hasText(borrowSearchQuery.getDescription())) {
            builder.withQuery(QueryBuilders.matchQuery("description", borrowSearchQuery.getDescription()));
        }
        if (borrowSearchQuery.getStartPeriod() != null) {
            builder.withQuery(QueryBuilders.rangeQuery("period").gte(borrowSearchQuery.getStartPeriod()));
        }
        if (borrowSearchQuery.getEndPeriod() != null) {
            builder.withQuery(QueryBuilders.rangeQuery("period").lte(borrowSearchQuery.getEndPeriod()));
        }

        // 当前页
        int current = borrowSearchQuery.getPageNo() - 1;
        // 页大小
        int size = borrowSearchQuery.getPageSize();
        // 设置分页查询
        builder.withPageable(PageRequest.of(current,size));
        // 排序字段：createDate
        builder.withSort(SortBuilders.fieldSort("createDate").order(SortOrder.DESC));

        // 搜索
        SearchHits<BorrowVO> searchHits =
                elasticsearchRestTemplate.search(builder.build(), BorrowVO.class);
        log.info("搜索结果总数：{}",searchHits.getTotalHits());

        // 获取搜索结果，并转换
        List<BorrowVO> borrowVos = searchHits.getSearchHits().stream()
                .map(SearchHit::getContent).collect(Collectors.toList());

        // 封装数据
        Pages<BorrowVO> pages = new Pages<>();
        pages.setList(borrowVos);
        pages.setPageSize(borrowSearchQuery.getPageSize());
        pages.setPageNum(borrowSearchQuery.getPageNo());
        pages.setTotal(searchHits.getTotalHits());
        return pages;
    }
}
